//
//  HMStatusListViewModel.swift
//  sinaweibo
//
//  Created by lst on 16/5/27.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit

class HMStatusListViewModel: NSObject {
    //定义模型数组
    lazy var statusViewModelArray: [HMStatusViewModel] = [HMStatusViewModel]()
    
    
    
    func loadData(finished: (isSuccess: Bool) -> () ) {
        HMNetworkTools.sharedTools.loadHomePageData { (responseObject, error) in
            if error != nil {
                //SVProgressHUD.showErrorWithStatus(AppErrorTip)
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
//            print(responseObject)
            //处理数据 AFN 已经将数据处理成了 OC之后数组或者字典
            guard let dict = responseObject as? [String : AnyObject] else {
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
            //print(dict)
            //根据 "statuses" 获取数组
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                //执行失败的回调
                finished(isSuccess: false)
                return
            }
//            print(array)
            //遍历数组 获取单个字典
            //实例化空数组
            var tempArray = [HMStatusViewModel]()
            for item in array {
                //字典转模型
                let s = HMStatus(dict: item)
                let viewModel = HMStatusViewModel(status: s)
                //添加到数组中
                tempArray.append(viewModel)
            }
            
            //赋值保存
            self.statusViewModelArray = tempArray
            //刷新数据
            //self.tableView.reloadData()
            //执行成功的回调
            finished(isSuccess: true)
        }
    }

}
